package io.itit.ecp.admin.provide.selected;

import io.itit.ecp.admin.common.utils.JsonUtils;
import io.itit.ecp.admin.server.entity.CarbonSinkEntity;
import io.itit.ecp.admin.server.service.ICarbonSinkService;
import io.itit.grass.common.provide.action.resp.SelectedResp;
import io.itit.grass.common.server.selected.ISelected;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;


@Component(value = CarbonSinkSelected.CODE)
@RequiredArgsConstructor
public class CarbonSinkSelected implements ISelected<SelectedResp> {

    public static final String CODE = "CarbonSinkSelected";

    private final ICarbonSinkService carbonSinkService;

    @Override
    public String code() {
        return CODE;
    }

    @Override
    public List<SelectedResp> get(String param) {
        CarbonSinkEntity carbonSinkEntity = JsonUtils.deserialize(param, CarbonSinkEntity.class);
        String sceneProjectId = Objects.nonNull(carbonSinkEntity) ? carbonSinkEntity.getSceneProjectId() : null;
        String sceneEdificeId = Objects.nonNull(carbonSinkEntity) ? carbonSinkEntity.getSceneEdificeId() : null;
        String sceneFloorId = Objects.nonNull(carbonSinkEntity) ? carbonSinkEntity.getSceneFloorId() : null;
        String sceneRoomId = Objects.nonNull(carbonSinkEntity) ? carbonSinkEntity.getSceneRoomId() : null;
        return carbonSinkService.lambdaQuery()
                .eq(StringUtils.isNotEmpty(sceneProjectId), CarbonSinkEntity::getSceneProjectId, sceneProjectId)
                .eq(StringUtils.isNotEmpty(sceneEdificeId), CarbonSinkEntity::getSceneEdificeId, sceneEdificeId)
                .eq(StringUtils.isNotEmpty(sceneFloorId), CarbonSinkEntity::getSceneFloorId, sceneFloorId)
                .eq(StringUtils.isNotEmpty(sceneRoomId), CarbonSinkEntity::getSceneRoomId, sceneRoomId)
                .orderByAsc(CarbonSinkEntity::getCreateTm).list()
                .stream()
                .map(item -> SelectedResp.builder()
                        .label(item.getType())
                        .value(item.getId()).build())
                .collect(Collectors.toList());
    }
}
